//
//  XKMeMoViewController.m
//  SecurityNote
//
//  Created by joonsheng on 14-8-15.
//  Copyright (c) 2019年 XuKai. All rights reserved.
//

#import "XKMeMoViewController.h"
#import "XKAddMemoViewController.h"
#import "XKMemo.h"
#import "XKDatePickerView.h"
#import "XKEditMemoViewController.h"
#import "DHDeviceUtil.h"

@interface XKMeMoViewController ()<UITableViewDelegate,UITableViewDataSource>

//主表格
@property (nonatomic, weak) UITableView * memoTable;

//全部数据库条数
@property (nonatomic, strong) NSMutableArray * memoLists;

@property (nonatomic, strong) XKMemo * memoNote;

@property (nonatomic, weak) UIButton * addBtn;

@end

@implementation XKMeMoViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if (@available(iOS 11.0, *)) {
        self.navigationController.navigationBar.prefersLargeTitles = YES;
        self.navigationController.navigationBar.largeTitleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor]};
    }
    
    UITableView * memoTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    memoTable.separatorColor = MainColor;
    memoTable.rowHeight = 60;
    
    memoTable.delegate = self;
    memoTable.dataSource = self;
    
    self.memoTable = memoTable;
    [self.view addSubview:memoTable];
    
    CGFloat top = (MACRO_IS_IPHONE_X ? 150 : 120);
    UIButton * add = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width - 55, self.view.frame.size.height - top,  48, 48)];
    
    [add setBackgroundColor:MainColor];
    [add setTitle:@"+" forState:(UIControlStateNormal)];
    [add setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    add.titleLabel.font = [UIFont boldSystemFontOfSize:30];
    ViewRadius(add, 24);
    
    [add addTarget:self action:@selector(addNew:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:add];
    
    self.addBtn = add;
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(kk_panView:)];
    [self.addBtn addGestureRecognizer:pan];
    
    
    self.memoLists = [self.memoNote queryWithNote];
    
}

- (void)kk_panView:(UIPanGestureRecognizer *)pan
{

    CGPoint translation = [pan translationInView:pan.view];
    CGPoint center = pan.view.center;
    center.x += translation.x;
    center.y += translation.y;
    pan.view.center = center;
    
    [pan setTranslation:CGPointZero inView:pan.view];
}

-(XKMemo *)memoNote
{
    if (_memoNote == nil)
    {
        _memoNote = [[XKMemo alloc]init];
    }
    
    return _memoNote;
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    self.memoLists = [self.memoNote queryWithNote];
    [self.memoTable reloadData];
}


-(void)addNew:(UIImageView* )add
{
    [UIImageView animateWithDuration:0.3 animations:^{
        
        add.layer.transform = CATransform3DMakeScale(2, 2, 0);
        add.alpha = 0;
    }
    completion:^(BOOL finished)
     {
         XKAddMemoViewController *toAddController = [[XKAddMemoViewController alloc]init];
         
         [self presentViewController:toAddController animated:YES completion:^{
             
             add.layer.transform = CATransform3DIdentity;
             add.alpha = 1;

         }];
         
     }];
    
}


//列表数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.memoLists count];
}


//cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString * diaryID = @"diaryID";
    UITableViewCell * cell  = [tableView dequeueReusableCellWithIdentifier:diaryID];
    
    if (cell == nil)
    {
        cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle
                                    reuseIdentifier:diaryID];
        
    }
    
    
    //cell被选中的颜色
    cell.selectedBackgroundView = [[UIView alloc]initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = MainColor;
    //右侧的指示
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    //当前分区的数据
    self.memoNote = self.memoLists[[indexPath row]];
    
    cell.textLabel.text = self.memoNote.title;
    cell.textLabel.font = [UIFont boldSystemFontOfSize:23];
    //    cell.textLabel.textColor = MainColor;
    
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@%@    %@",self.memoNote.year, self.memoNote.time, self.memoNote.memotype];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
    
    return cell;
    
}



//编辑状态
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        self.memoNote = [self.memoLists objectAtIndex:[indexPath row]];
        
        [self.memoNote kk_deleteNote:self.memoNote.ids];
        
        [self.memoLists removeObjectAtIndex:[indexPath row]];
        
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
    
}



//选择的列表
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    XKEditMemoViewController * editController = [[XKEditMemoViewController alloc]init];
    
    self.memoNote = self.memoLists[[indexPath row]];
    
    editController.ids = self.memoNote.ids;
    
    self.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:editController animated:YES];
    
    self.hidesBottomBarWhenPushed = NO;
}

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    self.addBtn.hidden = YES;
    return @"删除";
}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    self.addBtn.hidden = NO;
    
    return YES;
}



@end
